package cn.edu.scau.dormitory_management_system.controller;

import cn.edu.scau.dormitory_management_system.entity.PermissionEntity;
import cn.edu.scau.dormitory_management_system.entity.UserEntity;
import cn.edu.scau.dormitory_management_system.entity.dto.PermissionDto;
import cn.edu.scau.dormitory_management_system.service.PermissionService;
import cn.edu.scau.dormitory_management_system.service.RolePermissionService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/admin/menu")
public class PermissionController {
    @Autowired
    PermissionService permissionService;
    @Autowired
    RolePermissionService rolePermissionService;
    @GetMapping("list")
    public Object list(HttpServletRequest request){
        UserEntity admin = (UserEntity) request.getAttribute("admin");
        List<PermissionEntity> list;
        if(admin.getUserId() == 0){
             list = permissionService.list();
        }
        else{
            list = permissionService.selectPermission(admin.getUserId());
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getPerms() == null || list.get(i).getPerms().equals("")){
                continue;
            }
            stringBuilder.append(list.get(i).getPerms());
            if(i != list.size() - 1){
                stringBuilder.append(",");
            }
        }
        JSONObject object = new JSONObject();
        object.put("permissions",stringBuilder.toString());
        object.put("menu",list);
        object.put("code",200);
        object.put("msg","查询成功");
        return object;
    }

    /**
     * 角色有的permission
     * @return
     */
    @GetMapping("listAll")
    public Object listAll(){
        List<PermissionEntity> list = permissionService.list();
        JSONObject object = new JSONObject();
        object.put("menu",list);
        object.put("code",200);
        object.put("msg","查询成功");
        return object;
    }

    /**
     * 删除
     * @param menuIds
     * @return
     */
    @PostMapping("delete")
    public Object delete(@RequestBody int[] menuIds){
        for (int menuId : menuIds) {
            permissionService.removeById(menuId);
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("permission_id",menuId);
            rolePermissionService.removeByMap(stringObjectHashMap);
        }
        JSONObject object = new JSONObject();
        object.put("code",200);
        object.put("msg","删除成功");
        return object;
    }
    /**
     * 删除
     * @return
     */
    @PostMapping("save")
    public Object save(@RequestParam(value = "type",required = true) int type,
                       @RequestParam(value = "name",required = true) String name,
                       @RequestParam(value = "parentId",required = false,defaultValue = "0") int parentId,
                       @RequestParam(value = "parentName",required = false) String parentName,
                       @RequestParam(value = "url",required = false) String url,
                       @RequestParam(value = "perms",required = false) String perms,
                       @RequestParam(value = "orderNum",required = false,defaultValue = "0") int orderNum,
                       @RequestParam(value = "icon",required = false) String icon,
                       @RequestParam(value = "isHome",required = false,defaultValue = "0") int isHome,
                       HttpServletRequest request){
        UserEntity admin = (UserEntity) request.getAttribute("admin");
        PermissionEntity permissionEntity = new PermissionEntity();
        permissionEntity.setType(type);
        permissionEntity.setName(name);
        permissionEntity.setParentId(parentId);
        permissionEntity.setParentName(parentName);
        permissionEntity.setUrl(url);
        permissionEntity.setPerms(perms);
        permissionEntity.setOrderNum(orderNum);
        permissionEntity.setIcon(icon);
        permissionEntity.setIsHome(isHome);
        permissionEntity.setCreateBy(String.valueOf(admin.getUserId()));
        Boolean flag = permissionService.isTrue(permissionEntity);
        JSONObject object = new JSONObject();
        if(flag){
            boolean save = permissionService.save(permissionEntity);
            if(save){
                object.put("code",200);
                object.put("msg","添加成功");
                return object;
            }
            object.put("code",0);
            object.put("msg","添加失败");
            return object;
        }
        object.put("code",400);
        object.put("msg","有参数未填");
        return object;
    }
    /**
     * 删除
     * @return
     */
    @PostMapping("update")
    public Object update(@RequestParam(value = "menuId",required = true) int menuId,
                        @RequestParam(value = "type",required = true) int type,
                       @RequestParam(value = "name",required = true) String name,
                       @RequestParam(value = "parentId",required = true,defaultValue = "0") int parentId,
                       @RequestParam(value = "parentName",required = false) String parentName,
                       @RequestParam(value = "url",required = false) String url,
                       @RequestParam(value = "perms",required = false) String perms,
                       @RequestParam(value = "orderNum",required = false) int orderNum,
                       @RequestParam(value = "icon",required = false) String icon,
                         @RequestParam(value = "isHome",required = false,defaultValue = "1") int isHome,
                       HttpServletRequest request){
        UserEntity admin = (UserEntity) request.getAttribute("admin");
        PermissionEntity permissionEntity = new PermissionEntity();
        permissionEntity.setMenuId(menuId);
        permissionEntity.setType(type);
        permissionEntity.setIsHome(isHome);
        permissionEntity.setName(name);
        permissionEntity.setParentId(parentId);
        permissionEntity.setParentName(parentName);
        permissionEntity.setUrl(url);
        permissionEntity.setPerms(perms);
        permissionEntity.setOrderNum(orderNum);
        permissionEntity.setIcon(icon);
        permissionEntity.setLastUpdateBy(String.valueOf(admin.getUserId()));
        Boolean flag = permissionService.isTrue(permissionEntity);
        JSONObject object = new JSONObject();
        if(flag){
            boolean update = permissionService.updateById(permissionEntity);
            if(update){
                object.put("code",200);
                object.put("msg","修改成功");
                return object;
            }
            object.put("code",0);
            object.put("msg","修改失败");
            return object;
        }
        object.put("code",400);
        object.put("msg","有参数未填");
        return object;
    }
}
